﻿

local_web_wnd = {
    opts_tlb = {};
}

function local_web_wnd.init()
end;

function local_web_wnd.mini(uilib, ctrl_id)
    base_utility.log("[local_web_wnd]mini, ctrl_id:" .. tostring(ctrl_id));
    local win_ctn_id = ui_utility.get_ctrl_parent_id(ctrl_id);
    local win_id = ui_utility.get_ctrl_parent_id(win_ctn_id);
    if win_id > 0 then
        local pControl = uilib:GetControlPtr(win_id);
        local pWebDlg = tolua.cast(pControl, "Tenio::ITDialog");
        pWebDlg:Minimize();
    end;
end;

function local_web_wnd.close(uilib, ctrl_id)
    base_utility.log("[local_web_wnd]close, ctrl_id:" .. tostring(ctrl_id));
    local win_id = ui_utility.get_ctrl_parent_id(ctrl_id);
    local ctrl_ptr = ui_utility.get_ctrl_ptr_by_id(win_id);
    if nil ~= ctrl_ptr then
        local type_id = ctrl_ptr:GetTypeID();
        if type_id ~= ui_utility.kTCONID_DIALOG then
            win_id = ui_utility.get_ctrl_parent_id(win_id);
        end
    end
    local_web_wnd.dlg_close(uilib, win_id);
end;

function local_web_wnd.dlg_close(uilib, ctrl_id)
    base_utility.log("[local_web_wnd]dlg_close, ctrl_id:" .. tostring(ctrl_id));

    local opt = local_web_wnd.opts_tlb[ctrl_id];

    if opt and opt.win_close_callback then
        base_utility.log("[local_web_wnd][dlg_close]callback, ctrl_id:" .. tostring(ctrl_id));
        opt.win_close_callback(uilib, ctrl_id);
    end

    local_web_wnd.opts_tlb[ctrl_id] = nil;
end;

function local_web_wnd.layout(win_id, opts, url_param)
    base_utility.log("[local_web_wnd]layout, url:" .. tostring(opts.url));
    local_web_wnd.opts_tlb[win_id] = opts;
    -- 标题栏
    if opts.title then
        ui_utility.setproperty_by_id(win_id, 'Caption', opts.title);
        local title_id = ui_utility.get_ctrl_id_by_path(win_id, "TitleBarCtn\\Static_title");
        ui_utility.setproperty_by_id(title_id, 'Caption', opts.title);
    end

    local width = tonumber(opts.width);
    local height = tonumber(opts.height);

    local pDlg = ui_utility.get_ctrl_ptr_by_id(win_id);
    if nil ~= pDlg then
        local rtTemp = pDlg:GetRect(TRUE);
        rtTemp.left = rtTemp.left + (rtTemp.right  - rtTemp.left - width) / 2;
        rtTemp.top = rtTemp.top   + (rtTemp.bottom - rtTemp.top  - height) / 2;
        rtTemp.right = rtTemp.left + width; -- + 2
        rtTemp.bottom = rtTemp.top + height; -- + 42
        pDlg:Move(rtTemp);
    end;
              
    if opts.url ~= nil and opts.url ~= "" then
        local url = opts.url;
        if string.find(url, "(%w+)%?(%w+)=(%w+)") then
            url = url .. "&";
        else
            url = url .. "?";
        end
        -- concatenate the 'win_id'
        url = url .. "win_id=" .. win_id;
        if url_param ~= nil then
            for key, value in pairs(url_param) do
                if value then
                    url = url .. "&" .. key .. "=" .. tostring(value);
                end
            end
        end
        local const_ui_str, str_holder = U8toCUIStr(url);
        local web_id = ui_utility.get_ctrl_id_by_path(win_id, "web_container");
        local pQBWeb = ui_utility.get_ctrl_ptr_by_id(web_id);
        base_utility.log("[local_web_wnd]Navigate, url:" .. tostring(url));
        pQBWeb:Navigate(const_ui_str);
    end
end;

function local_web_wnd.OnTitleChanged(uilib, title, control_id)
end;

function local_web_wnd.OnJsCallback(uilib, fuc_name, json, control_id)
    base_utility.log("[local_web_wnd]OnJsCallback, " .. tostring(fuc_name) ..
        ", " .. base_utility.table_to_json(json) ..
        ", ctrl_id:" .. tostring(control_id));
    local control = uilib:GetControlPtr(control_id);
    local component = tolua.cast(control, "Tenio::IQBWebBrowser");
    local const_func_name, str_func_name = U8toCUIStr(fuc_name);
    local const_ui_str, str_holder = U8toCUIStr(json);
    local win_id = ui_utility.get_ctrl_parent_id(control_id);

    if win_id > 0 then
        local opts = local_web_wnd.opts_tlb[win_id];
        if not opts then
            base_utility.log("[local_web_wnd][]OnJsCallback]opts NOT found");
            return;
        end
        if not opts.js_call_lua then
            base_utility.log("[local_web_wnd][]OnJsCallback]js_call_lua NOT exist");
            return;
        end
        for key, value in pairs(opts.js_call_lua) do
            if key == fuc_name then
                local params = base_utility.json_to_table(json);
                opts.js_call_lua[key](params);
            end
        end
    end
end;

function local_web_wnd.OnNavigateComplete(uilib, url, control_id)
    base_utility.log("[local_web_wnd]OnNavigateComplete, url:" .. tostring(url));
    local win_id = ui_utility.get_ctrl_parent_id(control_id);
    local opt = local_web_wnd.opts_tlb[win_id];

    -- 明确从web触发显示
    if opt and true == opt.show_from_web then
        base_utility.log("[local_web_wnd][OnNavigateComplete]don't show now");
        return;
    end

    local_web_wnd.ShowWnd(uilib, control_id);
end;

function local_web_wnd.OnNavigateFailed(uilib, url, control_id)
    base_utility.log("[local_web_wnd]OnNavigateFailed, url:" .. tostring(url));
    local_web_wnd.ShowWnd(uilib, control_id);
end;

function local_web_wnd.OnCrash(uilib, control_id)
    local_web_wnd.ShowWnd(uilib, control_id);
end;


function local_web_wnd.ShowWnd(uilib, control_id)
    base_utility.log("[local_web_wnd]ShowWnd, control_id:" .. tostring(control_id));
    local win_id = ui_utility.get_ctrl_parent_id(control_id);
    base_utility.log("[local_web_wnd][ShowWnd]got win_id:" .. tostring(win_id));
    if win_id > 0 then
        local opts = local_web_wnd.opts_tlb[win_id];
        if opts == 0 or opts == nil then
            return;
        end
        base_utility.log("[local_web_wnd][ShowWnd]delay_show_wnd:" ..
                tostring(opts.delay_show_wnd));
        if opts.delay_show_wnd then
            base_utility.log("[local_web_wnd]ShowWnd, show win_id:" ..
                    tostring(win_id));
            local uilib = ui_utility.uilib();
            local pControl = uilib:GetControlPtr(win_id);
            local pDlg = tolua.cast(pControl, "Tenio::ITDialog");
            pDlg:Show(TRUE);
            pDlg:BringToTop();
        end
    end
end;

-- web对窗口进行设置，并获取初始数据
function local_web_wnd.on_srv_localweb_setup(data_in)
    base_utility.log("[local_web_wnd]on_srv_localweb_setup " ..
            base_utility.table_to_json(data_in));

    if data_in.win_id == nil then
        base_utility.log("[local_web_wnd][on_srv_localweb_setup]win_id is nil, data_in:" ..
                base_utility.table_to_json(data_in), true);
        return {};
    end

    local win_id = tonumber(data_in.win_id);
    if 0 == win_id then
        base_utility.log("[local_web_wnd][on_srv_localweb_setup]win_id is 0, data_in:" ..
                base_utility.table_to_json(data_in), true);
        return {};
    end

    if data_in.width ~= nil and data_in.height ~= nil then
        local width = tonumber(data_in.width);
        local height = tonumber(data_in.height);
        ui_utility.control_resize_by_id(win_id, width, height);
    end

    local src_opt = local_web_wnd.opts_tlb[win_id];

    if src_opt == nil then
        return {};
    end

    if src_opt.web_ready_handle then
        base_utility.log("[local_web_wnd][on_srv_localweb_setup]call web_ready_handle");
        src_opt.web_ready_handle(win_id, src_opt);
    end

    local opt = base_utility.copy_table_without_function(src_opt);

    if opt == nil then
        base_utility.log("[local_web_wnd][on_srv_localweb_setup]opt NOT FOUND, data_in:" ..
                base_utility.table_to_json(data_in), true);
        return {};
    end

    return opt;
end;